package com.boroborome.convert;

import java.time.Instant;

public class NgnixCallRecord implements ICallRecord {
    private Instant logTime;
    private String url;
    private double time;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public Instant getLogTime() {
        return logTime;
    }

    @Override
    public String getFunctionId() {
        return url;
    }

    public double getTime() {
        return time;
    }

    public void setTime(double time) {
        this.time = time;
    }

    public static NgnixCallRecord parse(String[] items) {
        NgnixCallRecord record = new NgnixCallRecord();
        record.logTime = Instant.parse(items[0]);
        record.setUrl(parseUrl(items[1]));
        record.setTime(readTime(items[2]));
        return record;
    }


    private static String parseUrl(String item) {
        int questionMaskIndex = item.indexOf('?');

        return questionMaskIndex > 0
                ? item.substring(0, questionMaskIndex)
                : item;
    }

    private static double readTime(String item) {
        return Double.parseDouble(item);
    }

}
